package org.jeecg.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.AppUser;
import org.jeecg.domain.LoginDto;
import org.jeecg.service.AppUserService;
import org.jeecg.mapper.AppUserMapper;
import org.jeecg.utils.JwtToken;
import org.jeecg.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author Administrator
* @description 针对表【app_user】的数据库操作Service实现
* @createDate 2025-01-02 11:35:01
*/
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser>
    implements AppUserService{
    @Autowired
    private AppUserMapper appUserMapper;

    @Override
    public Result login(LoginDto dto) {
        if (dto.getPhone() == null || dto.getPassword() == null){
            return Result.error("账号或者密码不能为空");
        }
        AppUser appUser = appUserMapper.getUserByPhone(dto.getPhone(), dto.getPassword());
        if (appUser == null) {
            return Result.error("账号或者密码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", dto.getPhone());
        map.put("password", dto.getPassword());
        String token = JwtToken.createToken(map);

        return Result.success("登录成功", token).put("user", appUser);
    }
}




